package com.findbus.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.entity.Login;
import com.findbus.project.R;
import com.findbus.view.LoginDialog;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.util.tool.ApiConnHelp;
import com.util.tool.MD5Util;
import com.util.tool.ToastHelp;

public class LoginActivity extends FragmentActivity {
	@ViewInject(R.id.a_login_edit_name)
	private EditText edit_name;
	@ViewInject(R.id.a_login_edit_pass)
	private EditText edit_pass;
	private Handler handler;
	private Login login;
	private Resources resources;
	private int requestCode = 1001;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		resources = getResources();
		initData();
		initView();
	}

	@SuppressLint("HandlerLeak")
	private void initData() {
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				login = (Login) msg.obj;
				String msg1 = resources.getString(R.string.network_err);
				switch (login.getFlag()) {
				case -1:
					msg1 = resources.getString(R.string.login_err);
					break;
				case 0:
					msg1 = resources.getString(R.string.login_no);
					edit_name.setText("");
					edit_pass.setText("");
					break;
				case 1:
					if (msg.what == 1) {
						successToMain();
						return;
					}
					msg1 = resources.getString(R.string.login_wit);
					break;
				case 2:
					msg1 = resources.getString(R.string.login_isQ);
					showDislog(msg1);
					return;
				case 3:
					successToMain();
					return;
				}
				ToastHelp.toast(LoginActivity.this, msg1);
			}
		};
	}

	private void initView() {
		// TODO Auto-generated method stub
		ViewUtils.inject(this);
	}

	public void successToMain() {
		Intent intent = new Intent(this, MainActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("login", login);
		intent.putExtras(bundle);
		startActivity(intent);
		finish();
	}

	public void showDislog(String msg) {
		new LoginDialog.Builder(this).setMessage(msg).setPositiveButton("是", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						Message message = handler.obtainMessage();
						message.obj = ApiConnHelp.login_force(login.getId(), 0 + "");
						message.what = 1;
						handler.sendMessage(message);
					}
				}).start();
			}
		}).show();
	}

	public void clickButton(View v) {
		switch (v.getId()) {
		case R.id.a_login_button_login:
			final String phone = edit_name.getText() + "";
			final String pass = edit_pass.getText() + "";
			String msg = null;
			if (TextUtils.isEmpty(phone))
				msg = "用户名";
			else if (TextUtils.isEmpty(pass))
				msg = "密码";
			if (msg != null) {
				ToastHelp.toast(this, msg + "不能为空");
				return;
			}
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					Message message = handler.obtainMessage();
					message.obj = ApiConnHelp.login(phone, MD5Util.generatePassword(pass));
					handler.sendMessage(message);
				}
			}).start();
			break;
		case R.id.a_login_button_one:

			// break;
		case R.id.a_login_button_danwei:
			startActivityForResult(new Intent(this, RegisteActivity.class), requestCode);
			break;
		case R.id.a_login_text_lostpass:
			startActivityForResult(new Intent(this, LostPassActivity.class), requestCode);
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		edit_name.setText(arg2.getExtras().getString("phone"));
	}
}
